package com.wis.offline;

import com.alibaba.fastjson.JSONArray;
import com.wis.pool.redisCluster.MyJedisClusterPool;
import com.wis.tools.CommonUtiles;
import com.wis.tools.RiskUtiles;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.JedisCluster;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @Description: traffic_wis
 * @author: fan
 * @Date: Created in 2019/10/21 14:04
 * @Modified By:
 */
public class OfflineTools {
    private static RiskUtiles riskUtiles = new RiskUtiles();
    private static JSONArray jsonKaZhi = new JSONArray();
    // private static Map map;
    private static Map<String, String> kaZhiProMap = new HashMap<>();
    private static Map<String, String> kaZhiPointMap = new HashMap<>();
    private static JSONArray jsonArray = new JSONArray();
    private static List list = new LinkedList();
    private static Map<String, String> revisionMap = new HashMap();
    private static JedisCluster jedisCluster = null;

    public static String processDataByRow(String[] oneLine, String date, int[] positionArr, Properties properties, String typeProduce) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        LinkedList<Integer> complex = new LinkedList<>();
        boolean flag = false;
        // 日期
        jsonKaZhi = CommonUtiles.jsonKaZhiSelect(date, oneLine[0], list, kaZhiPointMap, kaZhiProMap, jsonArray);
        // TODO 能见度
        for (int i = positionArr[2]; i < positionArr[2] + positionArr[positionArr.length - 1]; i++) {
            // 判断是否是订正过的桩点
            if (list.contains("2-" + oneLine[0])) {
                // 修正后的值
                // key:2-G10_101        value:201908221010-3
                String mergeKey = "2-" + oneLine[0];
                String[] temRevisionValue = revisionMap.get(mergeKey).split("-", -1);
                boolean flagCalc = CommonUtiles.isFlagCalc(date, temRevisionValue[0], i - positionArr[2]);
                if (flagCalc) {
                    // 此次计算时间没有过期
                    linkedList.add(Integer.parseInt(temRevisionValue[1]));
                } else {
                    jsonKaZhi = CommonUtiles.jsonKaZhiRevSelect(date, list, kaZhiProMap, jsonArray);
                    linkedList.add(Integer.parseInt(riskUtiles.VBKaZhi(jsonKaZhi.getJSONObject(4), oneLine[i])));
                    if (i == positionArr[2])
                        CommonUtiles.deleteRevKey(jedisCluster, properties.getProperty("redis.key.risk.revision"), (mergeKey));
                    jsonKaZhi = CommonUtiles.jsonKaZhiSelect(date, oneLine[0], list, kaZhiPointMap, kaZhiProMap, jsonArray);
                }
            } else {
                linkedList.add(Integer.parseInt(riskUtiles.VBKaZhi(jsonKaZhi.getJSONObject(4), oneLine[i])));
            }
            complex.add(linkedList.getLast());
        }
        // TODO 风速
        for (int i = positionArr[3]; i < positionArr[3] + positionArr[positionArr.length - 1]; i++) {
            if (list.contains("3-" + oneLine[0])) {
                String mergeKey = "3-" + oneLine[0];
                String[] temRevisionValue = revisionMap.get(mergeKey).split("-", -1);
                boolean flagCalc = CommonUtiles.isFlagCalc(date, temRevisionValue[0], i - positionArr[3]);
                if (flagCalc) {
                    // 此次计算时间没有过期
                    linkedList.add(Integer.parseInt(temRevisionValue[1]));
                } else {
                    jsonKaZhi = CommonUtiles.jsonKaZhiRevSelect(date, list, kaZhiProMap, jsonArray);
                    linkedList.add(Integer.parseInt(riskUtiles.commonKaZhi(jsonKaZhi.getJSONObject(5), oneLine[i], oneLine[i - positionArr[positionArr.length - 1]])));
                    if (i == positionArr[3])
                        CommonUtiles.deleteRevKey(jedisCluster, properties.getProperty("redis.key.risk.revision"), (mergeKey));
                    jsonKaZhi = CommonUtiles.jsonKaZhiSelect(date, oneLine[0], list, kaZhiPointMap, kaZhiProMap, jsonArray);
                }
            } else {
                linkedList.add(Integer.parseInt(riskUtiles.commonKaZhi(jsonKaZhi.getJSONObject(5), oneLine[i], oneLine[i - positionArr[positionArr.length - 1]])));
            }
            if (linkedList.getLast() > complex.get(i - positionArr[3])) {
                complex.set(i - positionArr[3], linkedList.getLast());
            }
        }
        // TODO 降水
        for (int i = positionArr[5]; i < positionArr[5] + positionArr[positionArr.length - 1]; i++) {
            int njdPosition = i - 3 * positionArr[positionArr.length - 1];
            if (list.contains("1-" + oneLine[0])) {
                String mergeKey = "1-" + oneLine[0];
                String[] temRevisionValue = revisionMap.get(mergeKey).split("-", -1);
                boolean flagCalc = CommonUtiles.isFlagCalc(date, temRevisionValue[0], i - positionArr[5]);
                if (flagCalc) {
                    // 此次计算时间没有过期
                    if (oneLine[i + positionArr[positionArr.length - 1]].contains("302")) {
                        linkedList.add(10 + Integer.parseInt(temRevisionValue[1]));
                    } else {
                        linkedList.add(Integer.parseInt(temRevisionValue[1]));
                    }
                } else {
                    jsonKaZhi = CommonUtiles.jsonKaZhiRevSelect(date, list, kaZhiProMap, jsonArray);
                    if (typeProduce.contains("3")) {
                        if (oneLine[i + positionArr[positionArr.length - 1]].contains("6")) {
                            linkedList.add(Integer.parseInt(riskUtiles.sleetKaZhi(jsonKaZhi.getJSONObject(3), oneLine[i - 3 * positionArr[positionArr.length - 1]])));
                        } else if (oneLine[i + positionArr[positionArr.length - 1]].contains("301")) {
                            linkedList.add(Integer.parseInt(riskUtiles.commonKaZhi(jsonKaZhi.getJSONObject(1), oneLine[i], oneLine[njdPosition])));
                        } else if (oneLine[i + positionArr[positionArr.length - 1]].contains("302")) {
                            linkedList.add(10 + Integer.parseInt(riskUtiles.commonKaZhi(jsonKaZhi.getJSONObject(3), oneLine[i], oneLine[njdPosition])));
                        } else {
                            linkedList.add(0);
                        }
                    } else if (typeProduce.contains("1") || typeProduce.contains("2")) {
                        if (oneLine[i + positionArr[positionArr.length - 1]].contains("6")) {
                            linkedList.add(Integer.parseInt(riskUtiles.sleetKaZhi(jsonKaZhi.getJSONObject(2), oneLine[njdPosition])));
                        } else if (oneLine[i + positionArr[positionArr.length - 1]].contains("301")) {
                            linkedList.add(Integer.parseInt(riskUtiles.commonKaZhi(jsonKaZhi.getJSONObject(0), oneLine[i], oneLine[njdPosition])));
                        } else if (oneLine[i + positionArr[positionArr.length - 1]].contains("302")) {
                            linkedList.add(10 + Integer.parseInt(riskUtiles.commonKaZhi(jsonKaZhi.getJSONObject(2), oneLine[i], oneLine[njdPosition])));
                        } else {
                            linkedList.add(0);
                        }
                    }
                    if (i == positionArr[5])
                        CommonUtiles.deleteRevKey(jedisCluster, properties.getProperty("redis.key.risk.revision"), (mergeKey));
                    jsonKaZhi = CommonUtiles.jsonKaZhiSelect(date, oneLine[0], list, kaZhiPointMap, kaZhiProMap, jsonArray);
                }
            } else {
                // 因为短临、实况和预报的卡值数据不同，一个是分钟级，一个是小时级别
                if (typeProduce.contains("3")) {
                    if (oneLine[i + positionArr[positionArr.length - 1]].contains("6")) {
                        linkedList.add(Integer.parseInt(riskUtiles.sleetKaZhi(jsonKaZhi.getJSONObject(3), oneLine[njdPosition])));
                    } else if (oneLine[i + positionArr[positionArr.length - 1]].contains("301")) {
                        linkedList.add(Integer.parseInt(riskUtiles.commonKaZhi(jsonKaZhi.getJSONObject(1), oneLine[i], oneLine[njdPosition])));
                    } else if (oneLine[i + positionArr[positionArr.length - 1]].contains("302")) {
                        linkedList.add(10 + Integer.parseInt(riskUtiles.commonKaZhi(jsonKaZhi.getJSONObject(3), oneLine[i], oneLine[njdPosition])));
                    } else {
                        linkedList.add(0);
                    }
                } else if (typeProduce.contains("1") || typeProduce.contains("2")) {
                    if (oneLine[i + positionArr[positionArr.length - 1]].contains("6")) {
                        linkedList.add(Integer.parseInt(riskUtiles.sleetKaZhi(jsonKaZhi.getJSONObject(2), oneLine[njdPosition])));
                    } else if (oneLine[i + positionArr[positionArr.length - 1]].contains("301")) {
                        linkedList.add(Integer.parseInt(riskUtiles.commonKaZhi(jsonKaZhi.getJSONObject(0), oneLine[i], oneLine[njdPosition])));
                    } else if (oneLine[i + positionArr[positionArr.length - 1]].contains("302")) {
                        linkedList.add(10 + Integer.parseInt(riskUtiles.commonKaZhi(jsonKaZhi.getJSONObject(2), oneLine[i], oneLine[njdPosition])));
                    } else {
                        linkedList.add(0);
                    }
                }
            }
            if (linkedList.getLast() >= 10 && (linkedList.getLast() - 10) > complex.get(i - positionArr[5])) {
                complex.set(i - positionArr[5], linkedList.getLast() - 10);
            } else if (linkedList.getLast() < 10 && linkedList.getLast() > complex.get(i - positionArr[5])) {
                complex.set(i - positionArr[5], linkedList.getLast());
            }
        }
        // TODO 气温
        for (int i = positionArr[7]; i < positionArr[7] + positionArr[positionArr.length - 1]; i++) {
            if (list.contains("5-" + oneLine[0])) {
                String mergeKey = "5-" + oneLine[0];
                String[] temRevisionValue = revisionMap.get(mergeKey).split("-", -1);
                boolean flagCalc = CommonUtiles.isFlagCalc(date, temRevisionValue[0], i - positionArr[5]);
                if (flagCalc) {
                    // 此次计算时间没有过期
                    linkedList.add(Integer.parseInt(temRevisionValue[1]));
                } else {
                    jsonKaZhi = CommonUtiles.jsonKaZhiRevSelect(date, list, kaZhiProMap, jsonArray);
                    linkedList.add(Integer.parseInt(riskUtiles.qiWenKaZhi(jsonKaZhi.getJSONObject(7), oneLine[i])));

                    if (i == positionArr[7])
                        CommonUtiles.deleteRevKey(jedisCluster, properties.getProperty("redis.key.risk.revision"), (mergeKey));
                    jsonKaZhi = CommonUtiles.jsonKaZhiSelect(date, oneLine[0], list, kaZhiPointMap, kaZhiProMap, jsonArray);
                }
            } else {
                linkedList.add(Integer.parseInt(riskUtiles.qiWenKaZhi(jsonKaZhi.getJSONObject(7), oneLine[i])));

            }
            if (linkedList.getLast() > complex.get(i - positionArr[7])) {
                complex.set(i - positionArr[7], linkedList.getLast());
            }
        }
        // TODO  结冰
        for (int i = positionArr[9]; i < positionArr[9] + positionArr[positionArr.length - 1]; i++) {
            if (list.contains("4-" + oneLine[0])) {
                String mergeKey = "4-" + oneLine[0];
                String[] temRevisionValue = revisionMap.get(mergeKey).split("-", -1);
                boolean flagCalc = CommonUtiles.isFlagCalc(date, temRevisionValue[0], i - positionArr[5]);
                if (flagCalc) {
                    // 此次计算时间没有过期
                    linkedList.add(Integer.parseInt(temRevisionValue[1]));

                } else {
                    jsonKaZhi = CommonUtiles.jsonKaZhiRevSelect(date, list, kaZhiProMap, jsonArray);
                    linkedList.add(Integer.parseInt(riskUtiles.RIKaZhi(jsonKaZhi.getJSONObject(6), oneLine[i], oneLine[i - 2 * positionArr[positionArr.length - 1]])));

                    if (i == positionArr[9])
                        CommonUtiles.deleteRevKey(jedisCluster, properties.getProperty("redis.key.risk.revision"), (mergeKey));
                    jsonKaZhi = CommonUtiles.jsonKaZhiSelect(date, oneLine[0], list, kaZhiPointMap, kaZhiProMap, jsonArray);
                }
            } else {
                linkedList.add(Integer.parseInt(riskUtiles.RIKaZhi(jsonKaZhi.getJSONObject(6), oneLine[i], oneLine[i - 2 * positionArr[positionArr.length - 1]])));

            }
            if (linkedList.getLast() > complex.get(i - positionArr[9])) {
                complex.set(i - positionArr[9], linkedList.getLast());
            }
        }
        if (linkedList.contains(1) || linkedList.contains(2) || linkedList.contains(3) || linkedList.contains(4)) {
            return oneLine[0] + "," + oneLine[1] + "," + StringUtils.join(linkedList, ",") + "," + StringUtils.join(complex, ",");
        } else {
            return null;
        }
    }

    public static void initMatchInfo(Properties properties) {
        try {
            if (jedisCluster == null) jedisCluster = MyJedisClusterPool.getRedisClusterObject();
            String hget = jedisCluster.hget(properties.getProperty("redis.key.kazhi"), "100000");
            if (StringUtils.isNotBlank(hget)) jsonKaZhi = JSONArray.parseArray(CommonUtiles.mapToJson(hget));
            jsonArray = jsonKaZhi;
            // 获取国省的所有卡值数据 // 获取详细所有点的卡值数据
            kaZhiProMap = jedisCluster.hgetAll(properties.getProperty("redis.key.kazhi"));// 返回的map集合
            // 判断redis是否存在该点的卡值信息
            if (jedisCluster.exists(properties.getProperty("redis.key.point.kazhi")))
                kaZhiPointMap = jedisCluster.hgetAll(properties.getProperty("redis.key.point.kazhi"));
            // 修正后的指标值获取
            Map<String, String> tmpRevisionMap = new HashMap<>();
            if (jedisCluster.exists(properties.getProperty("redis.key.risk.revision"))) {    // 判断是否存在需要修正的数据
                // 获取所有修正后点的数据
                revisionMap = jedisCluster.hgetAll(properties.getProperty("redis.key.risk.revision"));// 返回的map集合
            }
            if (revisionMap.size() > 0)
                for (Map.Entry m : revisionMap.entrySet()) {
                    String keyMap = m.getKey().toString();
                    list.add(keyMap);
                    tmpRevisionMap.put(keyMap, m.getValue().toString());
                }
            revisionMap = tmpRevisionMap;
            // MyJedisPool.returnJedisOjbect(jedis);
            // 省、市、县进行桩点卡值
            if (kaZhiProMap.size() > 1)
                for (Map.Entry m : kaZhiProMap.entrySet()) {
                    String keyMap = m.getKey().toString();
                    if (!keyMap.contains("100000")) {
                        list.add(keyMap);
                    }
                }
            // 具体桩点进行卡值
            if (kaZhiPointMap.size() > 0)
                for (Map.Entry m : kaZhiPointMap.entrySet()) {
                    list.add(m.getKey());
                }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyJedisClusterPool.returnJedisCluterObject(jedisCluster);
        }
    }

    //读取配置文件
    public static Properties getProperties(String confPath) {
        InputStream in = null;
        Properties prop = new Properties();
        try {
            // in = new FileInputStream(new File("").getAbsoluteFile() + "/" + "src/main/resources/offline.properties");
            in = OfflineTools.class.getClassLoader().getResourceAsStream(confPath);
            prop.load(in);//获取配置文件
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return prop;
    }
}
